package com.igeekhome.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.igeekhome.biz.IBlackListService;
import com.igeekhome.biz.ICustomerInfoService;
import com.igeekhome.biz.ICustomerServiceService;
import com.igeekhome.biz.ISessionService;
import com.igeekhome.pojo.BlackList;
import com.igeekhome.pojo.CustomerInfo;
import com.igeekhome.pojo.CustomerService;
import com.igeekhome.pojo.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 客服表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-20
 */
@Controller
@RequestMapping("/customerService")
public class CustomerServiceController {
    @Autowired
    private ICustomerServiceService iCustomerServiceService;

    @Autowired
    private ICustomerInfoService iCustomerInfoService;

    @Autowired
    private ISessionService iSessionService;

    @Autowired
    private IBlackListService iBlackListService;
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(CustomerService customerService, Model model, HttpSession session){

        String path = "";
        session.removeAttribute("loginmsg");
        String md5Pass = DigestUtils.md5DigestAsHex(customerService.getPassword().getBytes());
        customerService.setPassword(md5Pass);

        QueryWrapper<CustomerService> queryWrapper = new QueryWrapper();
        queryWrapper.eq("email",customerService.getEmail());
        queryWrapper.eq("password",customerService.getPassword());
        CustomerService cs = iCustomerServiceService.getOne(queryWrapper);

        if(cs != null){

            session.setAttribute("cs",cs);
           // session.setAttribute("pagesCount");
            LocalDateTime logintime = LocalDateTime.now();
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.set("state","在线");
            updateWrapper.set("onlinetimebegin",logintime);
            updateWrapper.eq("email",customerService.getEmail());
            updateWrapper.eq("password",customerService.getPassword());
            iCustomerServiceService.update(cs,updateWrapper);

            path = "redirect:/index";
        }else {

//            path = "/login";//1
//            model.addAttribute("msg","用户名或密码错误");
//            path = "forward:/login";//2 转发给其他控制器， 在根下找/login 执行
            session.setAttribute("loginmsg","用户名或密码错误");

            path = "redirect:/login";
        }

        return path;
    }
    @RequestMapping("/exit")
    public String exist(HttpSession session){


        CustomerService customerService = (CustomerService) session.getAttribute("cs");
        LocalDateTime exittime = LocalDateTime.now();
        LocalDateTime logintime = customerService.getOnlinetimebegin();
        Duration duration = Duration.between(logintime,exittime);
        long minutes = duration.toMinutes();
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("state","离线");
        updateWrapper.set("totalonlinetime",minutes);
        updateWrapper.set("onlinetimeend",exittime);
        updateWrapper.eq("email",customerService.getEmail());
        iCustomerServiceService.update(customerService,updateWrapper);
        session.removeAttribute("cs");

        return "login";
    }
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String register(CustomerService customerService, Model model, HttpSession session){

        session.removeAttribute("msg");
        QueryWrapper<CustomerService> queryWrapper = new QueryWrapper();
        queryWrapper.eq("email",customerService.getEmail());
        System.out.println(customerService);
        CustomerService cs = iCustomerServiceService.getOne(queryWrapper);
        if(cs != null)
        {
           session.setAttribute("msg","该邮箱已被注册过");
            return "/register";

        }else if(customerService.getEmail().equals("")||customerService.getPassword().equals("")||customerService.getRealname().equals("")){

            session.setAttribute("msg","注册信息不能为空");
            return "/register";
        }
        else {

            String md5Pass = DigestUtils.md5DigestAsHex(customerService.getPassword().getBytes());
            customerService.setPassword(md5Pass);
            boolean i = iCustomerServiceService.save(customerService);
            System.out.println(i);
            session.setAttribute("msg","注册成功");
            return "/register";
        }

    }
    @RequestMapping("/CustomerService")
    public String customerServicesMessage(Model model, Integer current, Integer size)
    {
        IPage<CustomerService> page=new Page<>(current,size);
        IPage<CustomerService> page1 = this.iCustomerServiceService.page(page);
        List<CustomerService> list = page1.getRecords();
        long pagesCount = page1.getPages();

        System.out.println(pagesCount+"**************************************************************************************");
        model.addAttribute("list",list);
        model.addAttribute("current",current);
        model.addAttribute("size",size);
        model.addAttribute("pagesCount",pagesCount);
        return "CustomerService";
    }
    @RequestMapping( value = "/csUpdate",method = RequestMethod.POST)
    public String CSUpdate(HttpSession session,CustomerService customerService) {
//        this.iCustomerServiceService.updateById(cs);
//        session.setAttribute("cs",cs);

        CustomerService cs;
        cs = (CustomerService) session.getAttribute("cs");
        CustomerService cs1;
        cs1= this.iCustomerServiceService.getById(cs.getCustomerserviceid());
        cs1.setRealname(customerService.getRealname());
        cs1.setEmail(customerService.getEmail());
        cs1.setCustomerservicephone(customerService.getCustomerservicephone());
        cs1.setServicegroup(customerService.getServicegroup());
        cs1.setServicerole(customerService.getServicerole());
        this.iCustomerServiceService.updateById(cs1);
        session.setAttribute("cs",cs1);
        return "CustomerSelf";
    }
    @RequestMapping("/Customer")
    public String customerMessage(Model model, Integer current, Integer size)
    {
        IPage<CustomerInfo> page=new Page<>(current,size);
        IPage<CustomerInfo> page1 = this.iCustomerInfoService.page(page);
        List<CustomerInfo> list = page1.getRecords();
        long pagesCount = page1.getPages();
        model.addAttribute("list",list);
        model.addAttribute("pagesCount",pagesCount);
        return "Customer";
    }
    @RequestMapping("/UnSession")
    public String sessionMessage(Model model, Integer current, Integer size)
    {
        IPage<Session> page=new Page<>(current,size);
        IPage<Session> page1 = this.iSessionService.page(page);
        List<Session> list = page1.getRecords();
        long pagesCount = page1.getPages();
        model.addAttribute("list",list);
        model.addAttribute("pagesCount",pagesCount);
        return "UnSession";
    }
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public String search(Model model,String id){

        IPage<CustomerService> page=new Page<>(1,8);
        IPage<CustomerService> page1 = this.iCustomerServiceService.page(page);
        List<CustomerService> list = page1.getRecords();
       //id查询
        CustomerService cs1 = this.iCustomerServiceService.getById(id);
        //姓名查询
        Map<String,Object> map1 = new HashMap<>();
        map1.put("realname",id);
        List<CustomerService> list1 =iCustomerServiceService.listByMap(map1);
        //状态查询
        Map<String,Object> map2 = new HashMap<>();
        map2.put("state",id);
        list1.addAll(iCustomerServiceService.listByMap(map2));
        if(cs1!=null || list1!=null) {
            list.clear();
            if(cs1!=null)list.add(cs1);
            list.addAll(list1);
            Set<CustomerService> set = new HashSet<>(list);
            model.addAttribute("list", set);
            model.addAttribute("pagesCount", 1);
        }

        return "CustomerService";
    }


}

